package com.gla.rabbitmq;


import com.gla.domain.Order;
import com.gla.domain.OrderDetailResponse;
import com.gla.enmutation.OrderStateEnum;
import com.gla.service.CommodityService;
import com.gla.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.amqp.core.Message;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Component
public class DelayConsumer {
    @Resource
    CommodityService commodityService;
    @Resource
    OrderService orderService;

    @RabbitListener(queues = {DelayConstant.DEAD_ORDER_QUEUE_NAME})
    public void orderDelayQueue(String order, Message message, Channel channel) {
        System.out.println("队列出："+order);
        //此处查看订单状态判断是否已经支付
        Integer id = Integer.parseInt(order);
        HashMap<String,Object> map = new HashMap<>();
        map.put("id",id);

        Order orderitem = orderService.select(map).get(0);
        //如果未支付，增加库存
        if(orderitem.getState() == Order.PLACED){
            orderService.updateStatus(id, Order.CANCELED);
            List<OrderDetailResponse> list = orderService.selectOrderDetailById(id);
            for(OrderDetailResponse item : list){
                commodityService.updateNumber(item.getCommodity().getId(),
                       item.getCommodity().getNumber() + item.getNumber());
                System.out.println("商品Id:"+item.getCommodity().getId()+"数量变为:"+item.getCommodity().getNumber() + item.getNumber());
            }
        }
    }

}